<?php

/**
 * Description of LeagueDao_Class
 *
 * @author aziz
 */
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/League_Class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Country_Class.php';
require_once 'GenericDAO_Class.php';
require_once 'CountryDAO_Class.php';

class LeagueDAO extends GenericDAO {

    public $countryDAO;

    public function __construct() {
        $this->countryDAO=new CountryDAO();
    }

    public function prepareService($module, $id, $object) {
        $this->type = LEAGUE_SERVICE_MOD;
        $this->module = $module;
        $this->id = $id;
        $this->object = $object;
        $this->__setUp();
    }

    public function loadAll() {
        $this->prepareService(SERVICE_ALL, null, null);
        $ligler = $this->getRequest();
        
        $leagues = array();
        for ($i = 0; $i < count($ligler); $i++) {
            $league = new League();
            $league->setId($ligler[$i]->id);
            $league->setName($ligler[$i]->name);
            $league->setCountry($this->countryDAO->loadById($ligler[$i]->country->id));
            $league->setStatus($ligler[$i]->status);
            $leagues[] = $league;
        }
        return $leagues;
    }

    public function loadById($id) {
        $this->prepareService(SERVICE_SINGLE, $id, null);
        $ligler = $this->getRequest();
        $league = new League();
        $league->setId($ligler->id);
        $league->setName($ligler->name);
        $league->setCountry($this->countryDAO->loadById($ligler->country->id));
        $league->setStatus($ligler->status);
        $league->setShortName($ligler->shortName);
        $league->setAbbreviation($ligler->abbreviation);
        return $league;
    }

    public function delete($id) {
        $this->prepareService(SERVICE_DELETE, $id, null);
        $this->deleteRequest();
        return $league;
    }

    public function upsert($league) {
        $this->prepareService(SERVICE_SAVE, null, $league);
        $this->postRequest();
        return $league;
    }

}

?>
